package com.ericdm.mydoubanmovie;

import android.os.Bundle;
import android.provider.Settings.System;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class TheWebView extends Activity {
	private WebView mMyWebView = null;
	private Button mBackButton = null;
	private Button mForwordButton = null;
	private Button mRefreshButton = null;
	private Button mExitButton = null;
	private String mMovieUrlString;
	
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_the_web_view);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar_detail_movie_info);
		mMovieUrlString = getIntent().getStringExtra("moviewUrl");
		mMyWebView = (WebView)findViewById(R.id.my_webview);
		mBackButton = (Button)findViewById(R.id.backword_button);
		mForwordButton = (Button)findViewById(R.id.foreword_button);
		mRefreshButton = (Button)findViewById(R.id.refresh_button);
		mExitButton = (Button)findViewById(R.id.exite_web_button);
		WebSettings myWebSettings = mMyWebView.getSettings();
		myWebSettings.setJavaScriptEnabled(true);
		mMyWebView.loadUrl(mMovieUrlString);
		mMyWebView.setWebViewClient(new MyWebViewClient());
		mBackButton.setOnClickListener(BackButtonClickListener);
		mForwordButton.setOnClickListener(ForwordButtonListener);
		mRefreshButton.setOnClickListener(RefreshButtonListener);
		mExitButton.setOnClickListener(ExitButtonListener);
	}
	
	private class MyWebViewClient extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			//return super.shouldOverrideUrlLoading(view, url);
			view.loadUrl(url);
			return true;
		}
	}
	
	
	
	android.view.View.OnClickListener ExitButtonListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
//			Intent intent = new Intent(TheWebView.this, LoginActivity.class);
//			startActivity(intent);
			onBackPressed();
		}
	};
	
	OnClickListener RefreshButtonListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mMyWebView.reload();
		}
	};
	

	android.view.View.OnClickListener ForwordButtonListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (mMyWebView.canGoForward()) {
				mMyWebView.goForward();
			}
		}
	};
	
	OnClickListener BackButtonClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (mMyWebView.canGoBack()) {
				mMyWebView.goBack();
			}
		}
	};
	

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && mMyWebView.canGoBack()) {
			mMyWebView.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.the_web_view, menu);
		return true;
	}
	
	

}
